<?php

$html = '';
if (count($data['errors']) == 0) {
    $html .= '<div class="userProfile"><h2><a href="index.php?p=user&user_id=' .
            $data['user']['user_id'] . '">' . htmlspecialchars($data['user']['username']) .
            '</a></h2></div>';

    $comments_count = count($data['user']['comments']);
    if ($comments_count > 0) {
        $html .= '<div id="commentsContainer">';
        $html .= '<p class="commentsCount"><span class="commentNumber">' . $comments_count . '</span> коментара</p>';
        foreach ($data['user']['comments'] as $comment) {
            $html .= '<div class="commentInfo"><span class="commentsBookTitle"><a href="index.php?p=book&book_id='
                    . $comment['book_id'] . '"></span>' . htmlspecialchars($comment['book_title']) . '</a> | '
                    . ' ' . $comment['creation_date'] . '</div>';
            $html .= '<div class="userText">' . htmlspecialchars($comment['content']) . '</div>';
            $comments_count--;
        }
        $html .= '</div>';
    } else {
        $html .= '<p class="commentsCount"><span class="commentNumber">0</span> коментара</p>';
    }
}

echo $html;